import pygame

from daxiong.bg import BGManage
from daxiong.const import SIZE
from daxiong.food import FoodManage
from daxiong.hero import HeroManage
from daxiong.music import MusicManage
from daxiong.ui import UIManage


class GameManage:
    def __init__(self):
        self.state = "ready"
        pygame.init()
        pygame.display.set_caption("我的游戏")
        logo = pygame.image.load("./image/favicon.ico")
        self.clock = pygame.time.Clock()
        pygame.display.set_icon(logo)
        self.screen = pygame.display.set_mode(size=SIZE)
        MusicManage.play_bg_music()

        self.bg_manage = BGManage(self)
        self.food_manage = FoodManage(self)
        self.hero_mange = HeroManage(self)
        self.ui_manage = UIManage(self)



    def check_event(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()

            if event.type == pygame.KEYUP and self.state == "gaming":
                if event.key == pygame.K_k:
                    self.state = "end"



    def check_colloder(self):
        if self.state == "gaming":
            # r = pygame.sprite.groupcollide(self.hero_mange.hero_group, self.food_manage.food_group, False, False)
            # if r:
            #     for v in r.values():
            #         for f in v:
            #             f.kill()

            r = pygame.sprite.spritecollide(self.hero_mange.hero, self.food_manage.food_group, False)
            if r:
                for f in r:
                    print(f)
                    f.kill()
                    self.food_manage.generate()
                    self.ui_manage.add_score()
                    MusicManage.play_sound()
    def update_draw(self):
        self.bg_manage.update()
        self.food_manage.update()
        self.hero_mange.update()
        self.ui_manage.update()


        pygame.display.flip()

    def run(self):
        while True:
            print("---------")
            self.clock.tick(20)
            self.check_event()
            self.check_colloder()
            self.update_draw()


gm = GameManage()
gm.run()
